package com.fss.cart.controller;

import com.fss.cart.domain.*;
import com.fss.cart.service.CartService;
import com.fss.common.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 贷款申请Controller
 * 
 * @author ruoyi
 * @date 2024-10-26
 */
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController
{
    @Autowired
    private CartService cartService;

    @PostMapping("/add")
    public void addCart(@RequestBody AddCartRequest request) {
        List<CartItem> list = request.getList();
        Long userId = request.getUserId();
//        System.out.println(userId);
//        for (CartItem item : list) {
//            System.out.println(item.toString());
//        }
           cartService.add(list, userId);
    }

    @GetMapping("/products/{cartId}")
    public List<CartProduct> getCartProductsByCartId(@PathVariable Long cartId) {
        System.out.println("Cart ID: " + cartId);
        return cartService.getCartProductsByCartId(cartId);
    }

    @GetMapping("/stock/{skuId}")
    public Stock getStockBySkuId(@PathVariable Long skuId) {
        //.out.println(skuId);
        //System.out.println("Stock: " + cartService.getStockBySkuId(skuId));
        return cartService.getStockBySkuId(skuId);
    }

    @PostMapping("/delete")
    public void deleteCartItems(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        cartService.deleteCartItems(ids);
    }

    @GetMapping("/update")
    public void updateCartItem(
            @RequestParam("id") Long id,
            @RequestParam("quantity") Integer quantity) {
        cartService.updateCartItem(id, quantity);

    }
}

